package com.bestcem.xm.user.util.converter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bestcem.xm.user.controller.vo.group.GroupLevelDisplayVO;
import com.bestcem.xm.user.controller.vo.group.GroupLevelUpperVO;
import com.bestcem.xm.user.controller.vo.group.UserGroupLevelOldVO;
import com.bestcem.xm.user.controller.vo.group.UserGroupLevelVO;
import com.bestcem.xm.user.service.dto.group.UserGroupLevelDTO;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 层级参数转换类
 *
 * @author panyun <yun.pan@idiaoyan.com>
 * @version v1.0
 * @date 2022/7/11 18:06
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class UserGroupLevelConverter {

    /**
     * 组装返回参数
     *
     * @param groupLevelDTOs 入参
     * @return java.util.List<com.bestcem.xm.user.controller.vo.group.UserGroupLevelVO>
     * @author panyun <yun.pan@idiaoyan.com>
     * @date 2022/4/29 17:32
     */
    public static List<UserGroupLevelVO> levelDTOToVO(List<UserGroupLevelDTO> groupLevelDTOs) {
        ArrayList<UserGroupLevelVO> levelVOs = new ArrayList<>();

        for (UserGroupLevelDTO groupLevelDTO : groupLevelDTOs) {
            GroupLevelDisplayVO displayVO = displayStringToVO(groupLevelDTO.getLevelDisplay());

            UserGroupLevelVO levelVO = new UserGroupLevelVO();
            levelVO.setGroupLevelId(groupLevelDTO.getGroupLevelId())
                    .setGroupLevelName(groupLevelDTO.getGroupLevelName())
                    .setOrgId(groupLevelDTO.getOrgId())
                    .setLevelChildrenId(groupLevelDTO.getLevelChildrenId())
                    .setLevelDisplay(displayVO)
                    .setLevelNum(groupLevelDTO.getLevelNum())
                    .setVersionId(groupLevelDTO.getVersionId());
            levelVOs.add(levelVO);
        }
        return levelVOs;
    }

    /**
     * 处理levelDisplay
     *
     * @param levelDisplay
     * @return com.bestcem.xm.user.controller.vo.group.GroupLevelDisplayVO
     * @author panyun <yun.pan@idiaoyan.com>
     * @date 2022/7/21 13:55
     */
    public static GroupLevelDisplayVO displayStringToVO(String levelDisplay){
        GroupLevelDisplayVO displayVO = JSON.parseObject(levelDisplay, GroupLevelDisplayVO.class);
        if(Objects.isNull(displayVO)){
            GroupLevelDisplayVO vo = new GroupLevelDisplayVO();
            vo.setAdmin(Boolean.FALSE);
            vo.setCode(Boolean.FALSE);
            vo.setTitle(Boolean.FALSE);
            return vo;
        }
        return displayVO;
    }

    /**
     * 组装返回参数
     *
     * @param groupLevelDTOs 入参
     * @return java.util.List<com.bestcem.xm.user.controller.vo.group.GroupLevelUpperVO>
     * @author panyun <yun.pan@idiaoyan.com>
     * @date 2022/4/29 17:32
     */
    public static List<GroupLevelUpperVO> levelDTOToUpperVO(List<UserGroupLevelDTO> groupLevelDTOs) {
        ArrayList<GroupLevelUpperVO> groupLevelUpperList = new ArrayList<>();

        for (UserGroupLevelDTO groupLevelDTO : groupLevelDTOs) {
            GroupLevelDisplayVO displayVO = displayStringToVO(groupLevelDTO.getLevelDisplay());

            GroupLevelUpperVO levelVO = new GroupLevelUpperVO();
            levelVO.setGroupLevelId(groupLevelDTO.getGroupLevelId())
                    .setGroupLevelName(groupLevelDTO.getGroupLevelName())
                    .setOrgId(groupLevelDTO.getOrgId())
                    .setLevelChildrenId(groupLevelDTO.getLevelChildrenId())
                    .setLevelDisplay(displayVO)
                    .setLevelNum(groupLevelDTO.getLevelNum())
                    .setVersionId(groupLevelDTO.getVersionId())
                    .setCreatorId(groupLevelDTO.getCreatorId())
                    .setCreateTime(groupLevelDTO.getCreateTime())
                    .setOperatorId(groupLevelDTO.getOperatorId())
                    .setUpdateTime(groupLevelDTO.getUpdateTime());
            groupLevelUpperList.add(levelVO);
        }
        return groupLevelUpperList;
    }

    /**
     * 组装返回参数
     *
     * @param groupLevelDTOs 入参
     * @return java.util.List<com.bestcem.xm.user.controller.vo.group.UserGroupLevelVO>
     * @author panyun <yun.pan@idiaoyan.com>
     * @date 2022/4/29 17:32
     */
    public static List<UserGroupLevelOldVO> levelDTOToOldVO(List<UserGroupLevelDTO> groupLevelDTOs) {
        ArrayList<UserGroupLevelOldVO> levelVOs = new ArrayList<>();
        if (CollectionUtils.isEmpty(groupLevelDTOs)) {
            return levelVOs;
        }
        for (UserGroupLevelDTO groupLevelDTO : groupLevelDTOs) {
            UserGroupLevelOldVO levelVO = new UserGroupLevelOldVO();
            levelVO.setId(groupLevelDTO.getGroupLevelId())
                .setCreateTime(groupLevelDTO.getCreateTime())
                .setCreatorId(groupLevelDTO.getCreatorId())
                .setId(groupLevelDTO.getGroupLevelId())
                .setLevel(groupLevelDTO.getLevelNum())
                .setNextId(groupLevelDTO.getLevelChildrenId())
                .setOrgId(groupLevelDTO.getOrgId())
                .setTitle(groupLevelDTO.getGroupLevelName())
                .setUpdateTime(groupLevelDTO.getUpdateTime())
                .setDeleted(false)
                .setDisplay(StringUtils.isEmpty(groupLevelDTO.getLevelDisplay()) ? new JSONObject()
                    : JSONObject.parseObject(groupLevelDTO.getLevelDisplay()));
            levelVOs.add(levelVO);
        }
        return levelVOs;
    }
}
